package org.hxyjs.service.impl;


import org.hxyjs.dao.BookskindDao;
import org.hxyjs.dto.DTO;
import org.hxyjs.dto.MessageEnum;
import org.hxyjs.entity.Bookskind;
import org.hxyjs.page.PageInfo;
import org.hxyjs.service.BookskindService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

@Service("BookskindService")
public  class BookskindServiceImpl implements BookskindService {
    @Resource
    BookskindDao bookskindDao;
    @Override
    public DTO<Bookskind> getAllBookskinds()  throws Exception{
        List<Bookskind> bookskinds = bookskindDao.getAllBookskinds();
        MessageEnum messageEnum  = Optional.ofNullable(bookskinds).isPresent()?MessageEnum.GETBOOKSKINDSUCCESS: MessageEnum.GETBOOKSKINDFAIL;
        return new DTO<Bookskind>(messageEnum.getCode(),messageEnum.getMessage(),bookskinds);
    }

    @Override
    public DTO<Bookskind> deleteBookskindByBkno(String bkno)  throws Exception{
        Integer result = bookskindDao.deleteBookskindByBkno(bkno);
        MessageEnum messageEnum  =result> 0?MessageEnum.DELETEBOOKSKINDSUCCESS:MessageEnum.DELETEBOOKSKINDFAIL;
        return new DTO<Bookskind>(messageEnum.getCode(),messageEnum.getMessage());
    }

    @Override
    public DTO<Bookskind> updateBookskindByBkno(Bookskind bookskind)  throws Exception{
        Integer result = bookskindDao.updateBookskindByBkno(bookskind);
        MessageEnum messageEnum  =result> 0?MessageEnum.UPDATEBOOKSKINDSUCCESS:MessageEnum.UPDATEBOOKSKINDFAIL;
        return new DTO<Bookskind>(messageEnum.getCode(),messageEnum.getMessage());
    }

    @Override
    public DTO<Bookskind> addBookskind(Bookskind bookskind) throws Exception {
        Integer result = bookskindDao.addBookskind(bookskind);
        MessageEnum messageEnum  = result> 0?MessageEnum.ADDBOOKSKINDSUCCESS:MessageEnum.ADDBOOKSKINDFAIL;
        return new DTO<Bookskind>(messageEnum.getCode(),messageEnum.getMessage());
    }

    @Override
    public DTO<Bookskind> getBookskindByPage(PageInfo pageInfo) throws Exception {
        List<Bookskind> bookskinds = bookskindDao.getBookskindByPage(pageInfo);
        MessageEnum messageEnum  = Optional.ofNullable(bookskinds).isPresent()?MessageEnum.GETBOOKSKINDSUCCESS: MessageEnum.GETBOOKSKINDFAIL;
        return new DTO<Bookskind>(messageEnum.getCode(),messageEnum.getMessage(),bookskinds,pageInfo);
    }
 @Override
    public DTO<Bookskind> selectBookskindWithbooks() throws Exception {
     List<Bookskind> bookskinds = bookskindDao.selectBookskindWithbooks();
        MessageEnum messageEnum  = Optional.ofNullable(bookskinds).isPresent()?MessageEnum.GETBOOKSKINDSUCCESS: MessageEnum.GETBOOKSKINDFAIL;
        return new DTO<Bookskind>(messageEnum.getCode(),messageEnum.getMessage(),bookskinds);
    }
}
